package stack;

import java.util.Scanner;

import static stack.calculate.isOper;
import static stack.calculate.priority;

public class calculate {
    public static void main(String[] args) {
        //创建一字符串

        String expresion = "5+6+10/5+7*7*7";
        //创建两个栈，数栈 符号栈
        ArrayStack numStack = new ArrayStack(10);
        ArrayStack operStack = new ArrayStack(10);

        //定义需要的相关变量
        int index = 0;

        char ch1 = expresion.charAt(0);
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        String keepNum="";
        //while
        while (true) {
            ch = expresion.substring(index, index + 1).charAt(0);//substring 返回String charAt返回char
            //判断ch是什么 ，然后做相应处理
            if (isOper(ch)) {
                if (operStack.isEmpty()) {
                    operStack.push(ch);

                }
                else {
                    int priority = priority(ch);
                    int priority1 = priority(operStack.peek());
                    if (priority <= priority1) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                      oper = operStack.pop();
                        int cal = cal(num1, num2, oper);
                        numStack.push(cal);
                        operStack.push(ch);
                    } else if (priority>priority(operStack.peek())){
                        operStack.push(ch);
                    }

                }
                index++;
            }
            else{
                keepNum+=ch;
                if (index==expresion.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }else {
                    if (isOper(expresion.substring(index+1,index+2).charAt(0))){
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum="";
                    }

                }
                index++;
                if (index>=expresion.length()){
                    break;
                }

            }
        }
        while (true){
            if (operStack.isEmpty()){
                break;
            }
            num1=numStack.pop();
            num2=numStack.pop();
            oper=operStack.pop();
            res=cal(num1,num2,oper);
            numStack.push(res);
        }
        int res2 = numStack.pop();
        System.out.println(res2);


    }

    /**
     * 比较运算符的优先级
     *
     * @param oper
     * @return
     */
    public static int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

    /**
     * 判断是否是运算符
     *
     * @param val
     * @return
     */
    public static boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    /**
     * @param num1
     * @param num2
     * @param oper
     * @return
     */
    public static int cal(int num1, int num2, int oper) {
        int res = 0;//res 用于存放计算结果
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;

            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }

}

/**
 * 前缀表达式 运算符位于操作数之前
 * 如（3+4）*5-6  -*+3456
 *
 *
 *从右向左扫描，将6543压入堆栈
 * 遇到运算符弹出数字栈的两个数，并且进行运算  返回数字栈
 *
 */
class calculatorBoLan {
    //返回运算符的优先级 用数字表示
    public static void Bolan(){
        String expresion="-*+3456";

        //创建两个栈，数栈 符号栈
        ArrayStack numStack = new ArrayStack(10);
        ArrayStack operStack = new ArrayStack(10);

        //定义需要的相关变量
        int index = 0;

        char ch1 = expresion.charAt(0);
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        String keepNum="";
        //while
        while (true) {
            ch = expresion.substring(index, index + 1).charAt(0);//substring 返回String charAt返回char
            //判断ch是什么 ，然后做相应处理
            if (isOper(ch)) {
                if (operStack.isEmpty()) {
                    operStack.push(ch);

                }
                else {
                    int priority = priority(ch);
                    int priority1 = priority(operStack.peek());
                    if (priority <= priority1) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        int cal = calculate.cal(num1, num2, oper);
                        numStack.push(cal);
                        operStack.push(ch);
                    } else if (priority>priority(operStack.peek())){
                        operStack.push(ch);
                    }

                }
                index++;
            }
            else{
                keepNum+=ch;
                if (index==expresion.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }else {
                    if (isOper(expresion.substring(index+1,index+2).charAt(0))){
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum="";
                    }

                }
                index++;
                if (index>=expresion.length()){
                    break;
                }

            }
        }
        while (true){
            if (operStack.isEmpty()){
                break;
            }
            num1=numStack.pop();
            num2=numStack.pop();
            oper=operStack.pop();
            res= calculate.cal(num1,num2,oper);
            numStack.push(res);
        }
        int res2 = numStack.pop();
        System.out.println(res2);

    }

}